import {
	MessageConstant
} from "zpub_http/constant/MessageConstant.js"
import {
	WhatConstant
} from "zpub_http/constant/WhatConstant.js"
import {
	HttpParamKeyValue
} from "zpub_svr/constant/HttpParamKeyValue.js"
import {
	HttpProtocol
} from "zpub_http/http/HttpProtocol.js"
import {
	BaseService
} from "zpub_http/service/BaseService.js"

/*
 * 功能：公共服务模块
 * 需要传入的键：
 * 传入的值类型：
 * 传入的值含义：
 * 是否必传 ：
 * 作者：郑朝军 on 2019/4/7 23:23
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 */
class PayService extends BaseService {

	static weixinPay(price) {
		uni.request({
			url: `https://unidemo.dcloud.net.cn/payment/wx/mp?code=232323&amount=1`,
			success: (res) => {
				console.log("pay request success", res);
				if (res.statusCode !== 200) {
					uni.showModal({
						content: "支付失败，请重试！",
						showCancel: false
					});
					return;
				}
				if (res.data.ret === 0) {
					console.log("得到接口prepay_id", res.data.payment);
					let paymentData = res.data.payment;
					uni.requestPayment({
						timeStamp: paymentData.timeStamp,
						nonceStr: paymentData.nonceStr,
						package: paymentData.package,
						signType: 'MD5',
						paySign: paymentData.paySign,
						success: (res) => {
							uni.showToast({
								title: "感谢您的赞助!"
							})
						},
						fail: (res) => {
							uni.showModal({
								content: "支付失败,原因为: " + res
									.errMsg,
								showCancel: false
							})
						},
						complete: () => {}
					})
				} else {
					uni.showModal({
						content: res.data.desc,
						showCancel: false
					})
				}
			},
			fail: (e) => {
				console.log("fail", e);
				uni.showModal({
					content: "支付失败,原因为: " + e.errMsg,
					showCancel: false
				})
			}
		})
	}

	static weixinPayOld(price) {
		uni.login({
			success: (e) => {
				console.log("login success", e);
				uni.request({
					url: `https://unidemo.dcloud.net.cn/payment/wx/mp?code=${e.code}&amount=1`,
					success: (res) => {
						console.log("pay request success", res);
						if (res.statusCode !== 200) {
							uni.showModal({
								content: "支付失败，请重试！",
								showCancel: false
							});
							return;
						}
						if (res.data.ret === 0) {
							console.log("得到接口prepay_id", res.data.payment);
							let paymentData = res.data.payment;
							uni.requestPayment({
								timeStamp: paymentData.timeStamp,
								nonceStr: paymentData.nonceStr,
								package: paymentData.package,
								signType: 'MD5',
								paySign: paymentData.paySign,
								success: (res) => {
									uni.showToast({
										title: "感谢您的赞助!"
									})
								},
								fail: (res) => {
									uni.showModal({
										content: "支付失败,原因为: " + res
											.errMsg,
										showCancel: false
									})
								},
								complete: () => {}
							})
						} else {
							uni.showModal({
								content: res.data.desc,
								showCancel: false
							})
						}
					},
					fail: (e) => {
						console.log("fail", e);
						uni.showModal({
							content: "支付失败,原因为: " + e.errMsg,
							showCancel: false
						})
					}
				})
			},
			fail: (e) => {
				console.log("fail", e);
				uni.showModal({
					content: "支付失败,原因为: " + e.errMsg,
					showCancel: false
				})
			}
		})
	}

	/*
	 * 查询属性<br/>
	 */
	static queryParcel(listener) {
		listener.showLoading({
			title: '正在查询***'
		});
		let protocol = new HttpProtocol();
		protocol
			.setService('SvrPubQueryControl')
			.setMethod('InitSvr')
			.addParam(HttpParamKeyValue.PARAM_KEY_CMD, HttpParamKeyValue.PARAM_VALUE_LOGIN)
			.addParam(HttpParamKeyValue.PARAM_KEY_MAJOR, "主类型")
			.addParam(HttpParamKeyValue.PARAM_KEY_MINOR, "子类型")
			.addParam("sys", "11")
			.post().then((data) => {
				let jo = this.responseData(data);
				if (this.isDataInvalid("queryParcel", jo, listener)) {
					return MessageConstant.MSG_EMPTY;
				}
				this.sendMessage("queryParcel", data, WhatConstant.WHAT_NET_DATA_SUCCESS, listener);
			})
			.catch((err) => {
				this.sendMessage("queryParcel", err, WhatConstant.WHAT_NET_DATA_FAIL, listener);
			});
	}
}

export {
	PayService
}
